package com.example.musicdemo.widget

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Typeface
import android.graphics.drawable.ColorDrawable
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View
import android.widget.TextView
import com.example.musicdemo.R
import com.example.musicdemo.util.DisplayUtil.dip2px

class SideBar : View {
    //触摸事件
    private var onTouchingLetterChangedListener: OnTouchingLetterChangedListener? = null

    //26个字母
    companion object {
        val b: Array<String> = arrayOf(
            "A", "B", "C", "D", "E", "F", "G", "H", "I",
            "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V",
            "W", "X", "Y", "Z", "#"
        )
    }

    //选中
    private var choose: Int = -1
    private val paint: Paint = Paint()
    private var mTvDialog: TextView? = null
    private var mContext: Context? = null


    constructor(context: Context?) : super(context) {
        mContext = context;
    }

    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) {
        mContext = context;
    }

    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ) {
        mContext = context;
    }


    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        // 获取焦点改变背景颜色.
        val singleHeight = height/ b.size
        for (i in b.indices){
            paint.color = resources.getColor(R.color.design_default_color_primary_dark)
            paint.color= Color.BLUE
            paint.typeface = Typeface.DEFAULT//字体
            paint.isAntiAlias = true
            paint.textSize = dip2px(mContext, 12F).toFloat()
            // 选中的状态
            if (i == choose) {
                paint.color = resources.getColor(R.color.yellow);
                paint.isFakeBoldText = true;
            }
            // x坐标等于中间-字符串宽度的一半.
            val xPos = width / 2 - paint.measureText(b[i]) / 2
            val yPos = (singleHeight * i + singleHeight).toFloat()
            canvas?.drawText(b[i], xPos, yPos, paint);
            paint.reset();// 重置画笔

        }
    }


    override fun dispatchTouchEvent(event: MotionEvent?): Boolean {
        val action : Int? = event?.action
        val y: Float? = event?.y;// 点击y坐标
        val oldChoose = choose
        val listener = onTouchingLetterChangedListener
        // 点击y坐标所占总高度的比例*b数组的长度就等于点击b中的个数.
        val c =  (y!! / height * b.size)
        when(action){
            MotionEvent.ACTION_UP->{
                setBackgroundDrawable(ColorDrawable(0x00000000))
                choose = -1
                invalidate()
                if (mTvDialog!= null){
                    mTvDialog?.visibility = INVISIBLE
                }
            }
            else ->{
                setBackgroundResource(R.drawable.sidebar_background)
                if (!oldChoose.equals(c)){
                    listener?.onTouchingLetterChanged(b[c.toInt()])
                    mTvDialog?.text = b[c.toInt()]
                    mTvDialog?.visibility = View.VISIBLE;

                }
                choose = c.toInt();
                invalidate();


            }
        }

        return true
    }

    /**
     * 为SideBar设置显示字母的TextView
     *
     * @param textDialog
     */
    public fun setTextView(textDialog: TextView) {
        this.mTvDialog = textDialog
    }

    fun setOnTouchingLetterChangedListener(onTouchingLetterChangedListener: OnTouchingLetterChangedListener) {
        this.onTouchingLetterChangedListener = onTouchingLetterChangedListener
    }


    interface OnTouchingLetterChangedListener {
        fun onTouchingLetterChanged(s: String)
    }
}